import { Body, Controller, Get, Post, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { UploadService } from './upload.service';

@Controller('upload')
export class UploadController {
  constructor(
    private readonly uploadService: UploadService,
  ) {
    
  }

  @Post('writeletter-xyq')
  @UseInterceptors(
    FilesInterceptor('files', 100, { // 'files' 是字段名，10 是最大文件数
      storage: diskStorage({
        destination: './uploads', // 文件保存路径
        filename: (req, file, callback) => {
          const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
          const ext = extname(file.originalname);
          const filename = `${file.fieldname}-${uniqueSuffix}${ext}`;
          callback(null, `${filename}`);
        },
      }),
    }),
  )
  async writeletter1(@UploadedFiles() files: Express.Multer.File[], @Body() body) {
    const message = body.message;
    await this.uploadService.saveMessage('xyq', message || '-', files.map(info => info.filename).join(','));
    return {
      message: '操作成功',
      success: true,
    };
  }

  @Post('writeletter-cyc')
  @UseInterceptors(
    FilesInterceptor('files', 100, { // 'files' 是字段名，10 是最大文件数
      storage: diskStorage({
        destination: './uploads', // 文件保存路径
        filename: (req, file, callback) => {
          const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
          const ext = extname(file.originalname);
          const filename = `${file.fieldname}-${uniqueSuffix}${ext}`;
          callback(null, `${filename}`);
        },
      }),
    }),
  )
  async writeletter2(@UploadedFiles() files: Express.Multer.File[], @Body() body) {
    const message = body.message;
    await this.uploadService.saveMessage('cyc', message || '-', files.map(info => info.filename).join(','));
    return {
      message: '操作成功',
      success: true,
    };
  }
}
